package engine.social
{
	//import com.facebook.graph.*;
	import engine.core.*;
	import flash.events.*;
	import flash.external.*;
	import flash.system.*;
	import flash.utils.*;
	
	public class RxSocial extends EventDispatcher
	{
		private var m_networkId:String;
		private var m_userId:String;
		private var m_accessToken:String;
		private var m_gotNetworkId:Boolean;
		private var m_socialNetworkId:String;
		private var m_socialUserId:String;
		private var m_socialAccessToken:String;
		private var m_gotSocialId:Boolean;
		private var m_localFriend:RxFriend;
		private var m_friends:Vector.<RxFriend>;
		private var m_otherFriends:Object;
		private var m_loadingFriends:Boolean = false;
		private var m_friendsLoaded:Boolean = false;
		private var m_timeoutCounter:uint;
		private var m_friendRequestCallbacks:Object;
		private static const FRIEND_REQUEST_URL:String = "/me/friends?fields=installed,name,first_name";
		private static var s_instance:RxSocial;
		
		public function RxSocial()
		{
			s_instance = this;
			this.m_otherFriends = {};
			this.m_friendRequestCallbacks = {};
			var delay:uint = 888;
			this.m_timeoutCounter = 10 * 1000 / delay;
			var timer:* = new Timer(delay);
			timer.addEventListener(TimerEvent.TIMER, this.OnTimer);
			timer.start();
			this.m_gotNetworkId = false;
			this.m_gotSocialId = false;
			Security.loadPolicyFile("https://fbcdn-profile-a.akamaihd.net/crossdomain.xml");
		}
		
		private function OnTimer(event:TimerEvent) : void
		{
			var _loc_2:Object = null;
			var _loc_3:String = null;
			var _loc_4:String = null;
			var _loc_5:String = null;
			var _loc_6:String = null;
			var _loc_7:String = null;
			var _loc_8:String = null;
			var _loc_9:String = null;
			var _loc_10:Boolean = false;
			var _loc_11:Timer = null;
			if (ExternalInterface.available)
			{
				if (!this.m_gotNetworkId)
				{
					try
					{
						_loc_2 = ExternalInterface.call("function(){return RAPI.user.net;}");
						_loc_3 = _loc_2 as String;
						_loc_2 = ExternalInterface.call("function(){return RAPI.user.id;}");
						_loc_4 = _loc_2 as String;
						_loc_2 = ExternalInterface.call("function(){return RAPI.user.token;}");
						_loc_5 = _loc_2 as String;
						if (_loc_3 && _loc_4 && _loc_5 != null)
						{
							this.m_networkId = _loc_3;
							this.m_userId = _loc_4;
							this.m_accessToken = _loc_5;
							this.m_gotNetworkId = true;
						}
					}
					catch (e:*)
					{
					}
				}
				if (!this.m_gotSocialId)
				{
					try
					{
						_loc_2 = ExternalInterface.call("function(){return RAPI.user.rumble_channel;}");
						_loc_6 = _loc_2 as String;
						_loc_2 = ExternalInterface.call("function(){return RAPI.user.facebook_userID;}");
						_loc_7 = _loc_2 as String;
						if (_loc_6 == "fb" && _loc_7 != null)
						{
							_loc_2 = ExternalInterface.call("function(){return FB._domain.graph;}");
							_loc_8 = _loc_2 as String;
							_loc_2 = ExternalInterface.call("function(){return RAPI.user.facebook_accessToken;}");
							_loc_9 = _loc_2 as String;
							if (_loc_8.charAt((_loc_8.length - 1)) == "/")
							{
								_loc_8 = _loc_8.slice(0, (_loc_8.length - 1));
							}
							this.m_socialNetworkId = _loc_6;
							this.m_socialUserId = _loc_7;
							this.m_socialAccessToken = _loc_9;
							this.m_gotSocialId = true;
						}
					}
					catch (e:*)
					{
					}
				}
				if (this.m_timeoutCounter > 0)
				{
					_loc_10 = false;
					if (this.m_gotNetworkId && this.m_gotSocialId)
					{
						_loc_10 = true;
					}
					this.m_timeoutCounter = this.m_timeoutCounter - 1;
					if (--this.m_timeoutCounter == 0)
					{
						_loc_10 = true;
					}
					if (_loc_10)
					{
						_loc_11 = event.target as Timer;
						if (_loc_11 != null)
						{
							_loc_11.stop();
							_loc_11.removeEventListener(TimerEvent.TIMER, this.OnTimer);
						}
						if (!this.m_gotNetworkId)
						{
							RxLog.Warning("RxSocial: no login user credentials discovered");
						}
						if (!this.m_gotSocialId)
						{
							RxLog.Warning("RxSocial: no social user credentials discovered");
						}
					}
				}
				if (this.IsSocialLoggedIn())
				{
					RxLog.Info("RxSocial: userId=" + this.m_socialNetworkId + ":" + this.m_socialUserId);
					this.LoadFriends();
				}
			}
		}
		
		public function LoadFriends(fun:Function = null) : void
		{
			if (this.m_loadingFriends)
			{
				return;
			}
			this.m_localFriend = RxFriend.CreateFriendFromObject({id:this.m_userId, name:"You", first_name:"You", installed:true});
			this.m_friendsLoaded = false;
			this.m_loadingFriends = this.MakeRequest(FRIEND_REQUEST_URL, null, this.OnFriendsLoaded, fun);
		}
		
		public function IsSocialLoggedIn() : Boolean
		{
			return this.m_socialUserId != null;
		}
		
		private function MakeRequest(param1:String, param2:Object = null, param3:Function = null, param4:Function = null) : Boolean
		{
			var _loc_5:String = null;
			if (ExternalInterface.available)
			{
				_loc_5 = param1 + "&access_token=" + escape(this.m_socialAccessToken);
				//Facebook.api(_loc_5, param3);
				return true;
			}
			return false;
		}
		
		private function OnFriendsLoaded(param1:Object, param2:Object) : void
		{
			var _loc_3:RxFriend = null;
			var _loc_4:uint = 0;
			var _loc_5:int = 0;
			if (param1)
			{
				if (!this.m_friends)
				{
					this.m_friends = new Vector.<RxFriend>;
				}
				else
				{
					this.m_friends.length = 0;
				}
				_loc_4 = param1.length;
				_loc_5 = 0;
				while (_loc_5 < _loc_4)
				{
					
					_loc_3 = RxFriend.CreateFriendFromObject(param1[_loc_5]);
					if (_loc_3 && _loc_3.GetInstalled())
					{
						this.m_friends.push(_loc_3);
					}
					_loc_5++;
				}
				this.m_friendsLoaded = true;
				if (hasEventListener(RxSocialEvent.FRIENDS_LOADED))
				{
					dispatchEvent(new RxSocialEvent(RxSocialEvent.FRIENDS_LOADED));
				}
			}
			else if (param2.hasOwnProperty("error"))
			{
				RxLog.Error("Failed to load friends list.  Error type: " + param2.error.type + ", Message: " + param2.error.message);
			}
			else
			{
				RxLog.Error("Failed to load friends list.");
			}
			this.m_loadingFriends = false;
		}
		
		public static function get instance() : RxSocial
		{
			return s_instance;
		}
		
		public function IsLoggedIn() : Boolean
		{
			return this.m_userId != null;
		}
		
		public function GetNetworkId() : String
		{
			return this.m_networkId;
		}
		
		public function GetUserId() : String
		{
			return this.m_userId;
		}
	}
}